package com.emmy.walnut.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.lang.reflect.Method;

/**
 * @ClassName SQLiteDatabaseHelper
 * @Description TODO
 * @Author zhouxuan
 * @Date 2020/02/15 9:45
 * @Version 1.0
 */
public class SQLiteDatabaseHelper extends SQLiteOpenHelper {

    private static SQLiteDatabaseHelper instance;

    private SQLiteDatabaseHelper(Context context){
        super(context, "MyDatabase.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Class clazz = this.getClass();
        for(int i = oldVersion;i<newVersion;i++){
            try {
                Method method = clazz.getMethod("upgradeFrom" + i);
                method.invoke(this);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void upgradeFrom1() {

    }

    public SQLiteDatabaseHelper getInstance(Context context){
        if (instance==null){
            synchronized (this){
                if (instance==null){
                    instance = new SQLiteDatabaseHelper(context);
                }
            }
        }
        return instance;
    }
}
